<script setup>
import {ref,onMounted} from 'vue'
import RecommendGoodsCard from "@/components/card/recommendGoodsCard.vue";
import {getCategoriesAPI} from "@/apis/home.js";
import {useCategoryStore} from "@/stores/category.js";
import router from "@/router/index.js";

const item=ref({
  imgUrl:'https://yanxuan-item.nosdn.127.net/2269a4a0b26a532b430c3fb6e93b83eb.png',
  title:'西班牙特级初榨橄榄油750ml',
  g_price:'999.99'
})

const categories=ref([])
const goods=ref(null)

const getCategories=async ()=>{
  if(useCategoryStore().isEmpty) {
    await useCategoryStore().getCategories()
  }
  categories.value = useCategoryStore().categories
}

//挂载完成时发送请求
onMounted(()=>{
  getCategories()
})


//获取图标路径
function getImageUrl(imageName) {
  return new URL(`../assets/img/${imageName}`, import.meta.url).href
}

const toCategory = (category) => {
  if (!category) {
    console.error('分类参数不能为空')
    return
  }
  router.push(`/category/${category}`)
}

</script>

<template>
  <div class="product-class">

    <ul class="categoryUL">

<!--      <li v-for="(item ,index) in categories" :key="index">-->
<!--        <img src="@/assets/img/apple.png"  align="texttop" class="goods-icon" alt="">-->


<!--        <span class="categoryName" @click="toCategory(item.FirstCategory)"-->
<!--              style="font-size: 16px">{{ item.FirstCategory }}</span>-->
<!--        <span class="categoryName" @click="toCategory(category)"-->
<!--              v-for="category in item.SecondCategory">&nbsp{{category}}&nbsp</span>-->


<!--        <div class="goodsPanel slideInLeft" :style="{top:`${-(index*50+1)}px`}">-->
<!--          <div class="goodsArea">-->
<!--            <div class="titleArea">-->
<!--              <span style="font-size: 20px;line-height:80px;color: #333333">分类推荐 </span>-->
<!--              <span style="font-size: 16px;color: #a5a8ab"> 根据您的购买或浏览记录推荐</span>-->
<!--            </div>-->

<!--            <el-empty v-show="goods===null" description="暂无数据"/>-->

<!--            <div class="dataArea">-->
<!--              <recommend-goods-card v-show="goods !==null" v-for="x in 6" :item="item"></recommend-goods-card>-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--      </li>-->

            <li>
              <img src="@/assets/img/9-1.png"  align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('禽畜养殖')" style="font-size: 16px">禽畜养殖</span>
              <div class="goodsPanel slideInLeft" style="top:1px;overflow: hidden;">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">禽畜养殖分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('禽畜养殖')">
                      <span class="mainCat">禽畜养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('葱姜蒜类')">
                      <span class="subCat">葱姜蒜类</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('叶菜类')">
                      <span class="subCat">叶菜类</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('食用菌')">
                      <span class="subCat">食用菌</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/4-1.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('水果养殖')" style="font-size: 16px">水果养殖</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">水果养殖分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('水果养殖')">
                      <span class="mainCat">水果养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('水果种苗')">
                      <span class="subCat">水果种苗</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('蔬菜种子')">
                      <span class="subCat">蔬菜种子</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('果树栽培')">
                      <span class="subCat">果树栽培</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/6-1.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('粮油作物种植')" style="font-size: 16px">粮油作物种植</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">粮油作物种植分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('粮油作物种植')">
                      <span class="mainCat">粮油作物种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('小麦种植')">
                      <span class="subCat">小麦种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('玉米种植')">
                      <span class="subCat">玉米种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('水稻种植')">
                      <span class="subCat">水稻种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('大豆种植')">
                      <span class="subCat">大豆种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('油菜种植')">
                      <span class="subCat">油菜种植</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/meat.png" align="texttop"  class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('苗木花草种植')" style="font-size: 16px">苗木花草种植</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">苗木花草种植分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('苗木花草种植')">
                      <span class="mainCat">苗木花草种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('花卉种植')">
                      <span class="subCat">花卉种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('苗木培育')">
                      <span class="subCat">苗木培育</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('草坪种植')">
                      <span class="subCat">草坪种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('园林绿化')">
                      <span class="subCat">园林绿化</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/fish.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('农资')" style="font-size: 16px">农资</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">农资分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('农资')">
                      <span class="mainCat">农资</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('肥料使用')">
                      <span class="subCat">肥料使用</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('农药防治')">
                      <span class="subCat">农药防治</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('农机设备')">
                      <span class="subCat">农机设备</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('种子种苗')">
                      <span class="subCat">种子种苗</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/rice.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('经济作物种植')" style="font-size: 16px">经济作物种植</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">经济作物种植分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('经济作物种植')">
                      <span class="mainCat">经济作物种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('茶叶种植')">
                      <span class="subCat">茶叶种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('中药材种植')">
                      <span class="subCat">中药材种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('烟草种植')">
                      <span class="subCat">烟草种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('棉花种植')">
                      <span class="subCat">棉花种植</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/5-1.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('蔬菜种植')" style="font-size: 16px">蔬菜种植</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">蔬菜种植分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('蔬菜种植')">
                      <span class="mainCat">蔬菜种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('叶菜种植')">
                      <span class="subCat">叶菜种植</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('根茎蔬菜')">
                      <span class="subCat">根茎蔬菜</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('瓜果蔬菜')">
                      <span class="subCat">瓜果蔬菜</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('食用菌')">
                      <span class="subCat">食用菌</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>

            <li>
              <img src="@/assets/img/5-1.png" align="texttop" class="goods-icon" alt="">
              <span class="categoryName" @click="toCategory('水产养殖')" style="font-size: 16px">水产养殖</span>
              <div class="goodsPanel slideInLeft" style="top:1px">
                <div class="goodsArea">
                  <div class="titleArea">
                    <span style="font-size: 20px;line-height:80px;color: #333333">水产养殖分类</span>
                    <span style="font-size: 16px;color: #a5a8ab">点击下方分类进行搜索</span>
                  </div>
                  <div class="categoryGrid">
                    <div class="categoryItem" @click="toCategory('水产养殖')">
                      <span class="mainCat">水产养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('淡水养殖')">
                      <span class="subCat">淡水养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('海水养殖')">
                      <span class="subCat">海水养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('虾蟹养殖')">
                      <span class="subCat">虾蟹养殖</span>
                    </div>
                    <div class="categoryItem" @click="toCategory('特种养殖')">
                      <span class="subCat">特种养殖</span>
                    </div>
                  </div>
                </div>
              </div>
            </li>
    </ul>
  </div>




</template>


<style scoped>
.product-class {
  box-shadow: 0 0 5px 0 rgba(144, 144, 144, 0.29);
  margin: 20px 20px 20px 0;
  height: 400px;
  width: 250px;
  border-radius: 5px;
  padding: 1px;
  background-color: rgb(255, 255, 255);
  z-index: 1;
  position: relative;
}

.goodsPanel{
  display: none;
  position: absolute;
  z-index: 20;
  top: 0;
  left: 250px;
  width: 700px;
  height: 350px;
  background-color: #fff;
  /* x偏移量 | y偏移量 | 阴影模糊半径 | 阴影扩散半径 | 阴影颜色 */
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
  border-radius: 0 10px 10px 0;
  border-left: none;
  padding: 14px;
  overflow: hidden;
  transition: box-shadow .2s ease, transform .2s ease;
}

.goodsArea{
  display: flex;
  flex-direction: column;
  cursor: default;
}

.titleArea{
  width: 100%;
  height: 60px;
  overflow: hidden;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 8px;
}

.dataArea{
  max-width: 680px;
  height: auto;
  overflow: hidden;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 16px;
  padding: 8px;
}

li:hover>div[class]{
  display: block;
}

li:hover {
  position: relative;
  z-index: 11;
}

li{
  line-height: 50px;
}

.goods-icon{
  height: 20px;
  width: 20px;
  margin-right: 5px;
}
.categoryName{
  cursor: pointer;
}
.categoryName:hover{
  color: #39bf3e;
}

.categoryUL{
  list-style: none;
  color: #333333;
  padding-left: 0;
  margin: 0;
  font-family: "Microsoft YaHei",serif;
  justify-content: center;
}

.categoryUL li{
  height: 50px;
  font-size: 14px;
  padding-left: 20px;
  margin: 0 auto;
}

.categoryUL li:hover{
  box-shadow: 0 4px 12px rgba(7, 203, 41, 0.2);
  background-color: rgba(7, 203, 41, 0.12);
  color: #39bf3e;
  cursor:pointer;
  border-radius: 6px 0 0 6px;
}


.goodsPanel{
  animation-duration: .35s ;
  animation-fill-mode: both ;
}

@keyframes slideInLeft {
  from {
    width: 120px;
    transform: translate3d(-6px, 0, 0) ;
    visibility: visible;
  }

  to {
    width: 720px;
    transform: translate3d(-6px, 0, 0);
  }
}

.slideInLeft {
  animation-name: slideInLeft;
}

.categoryGrid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  padding: 0;
  width: 100%;
  height: 260px;
  overflow: auto;
  scroll-behavior: smooth;
}

.categoryItem {
  background-color: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 8px 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.categoryItem:hover {
  background-color: #39bf3e;
  color: #fff;
  border-color: #39bf3e;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(57, 191, 62, 0.28);
}

.mainCat {
  font-size: 14px;
  font-weight: bold;
  color: #333;
}

.subCat {
  font-size: 13px;
  color: #666;
}

.categoryItem:hover .mainCat,
.categoryItem:hover .subCat {
  color: white;
}

/* 滚动条美化（WebKit） */
.categoryGrid::-webkit-scrollbar{
  width: 8px;
}
.categoryGrid::-webkit-scrollbar-thumb{
  background: rgba(0,0,0,0.18);
  border-radius: 8px;
}
.categoryGrid::-webkit-scrollbar-thumb:hover{
  background: rgba(0,0,0,0.28);
}
.categoryGrid::-webkit-scrollbar-track{
  background: transparent;
}
</style>

